<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools
          :node-data="company"
          :is-root="true"
          @add-depts="handleAddDepts"
        />
        <!-- 数组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          default-expand-all
        >
          <template #default="{data}">
            <tree-tools
              :node-data="data"
              :is-root="false"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>

      </el-card>
    </div>
    <!-- 对话框--弹层-->
    <!-- @close="dialogFn" -->
    <add-dept
      ref="addDeptDialog"
      :show-dialog.sync="showDialog"
      :depts-list="deptsList"
      :node-data="nodeData"
      @add-depts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: { TreeTools, AddDept },
  data() {
    return {
      loading: false,
      deptsList: [], // 列表格式
      nodeData: {},
      showDialog: false,
      company: {},
      departs: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 编辑展示（编辑）
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$refs.addDeptDialog.getEmployeeSimple()
      this.$refs.addDeptDialog.getDepartDetail(nodeData.id)
    },
    // 展示弹层（新增）
    handleAddDepts(nodeData) {
      // console.log(nodeData, '111111111')
      // 显示弹层
      this.showDialog = true
      this.nodeData = nodeData
    },
    // 关闭弹框（弹框问题）
    dialogFn(flag) {
      this.showDialog = flag
      console.log(this.$refs.deptForm)
    },
    // 列表展示（列表数据）
    async getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()
      console.log(depts)
      // 将非树状的列表转传到add-dept组件中，利于使用some的方法
      this.deptsList = depts
      this.departs = tranListToTreeData(depts, '')
      // console.log(companyName, 111111)
      this.company = {
        name: companyName,
        manager: '负责人',
        id: ''
      }
      this.loading = false
    }
  }

}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
